﻿--同步单个枚举
DECLARE @FN VARCHAR(550),
        @ClsID BIGINT,
        @UID VARCHAR(50);
--此处只需填入你要更新的枚举FullName，示例为标准产品的业务员类型枚举
SELECT @FN = '{ EnumFullName }';
SELECT @ClsID = Local_ID,
       @UID = ID
FROM UBF_MD_Class
WHERE FullName = @FN
      AND ClassType = 3;
SELECT @ClsID,
       @UID;
--先删除已有的数据
DELETE FROM UBF_Sys_ExtEnumType_Trl
WHERE ID = @ClsID
      OR ID IN
         (
             SELECT ID FROM UBF_Sys_ExtEnumType WHERE UID = @UID
         );
DELETE FROM UBF_Sys_ExtEnumType
WHERE ID = @ClsID
      OR UID = @UID;
DELETE FROM UBF_Sys_ExtEnumValue_Trl
WHERE ID IN
      (
          SELECT ID
          FROM UBF_Sys_ExtEnumValue
          WHERE ExtEnumType = @ClsID
                OR ExtEnumTypeUID = @UID
      );
DELETE FROM UBF_Sys_ExtEnumValue
WHERE ExtEnumType = @ClsID
      OR ExtEnumTypeUID = @UID;
--下面为插入最新数据
INSERT INTO UBF_Sys_ExtEnumType
SELECT Local_ID AS id,
       '20101111',
       'YonYou',
       '20101111',
       'YonYou',
       0,
       FullName AS code,
       IsExtend,
       ID AS uid,
       0
FROM UBF_MD_Class
WHERE Local_ID = @ClsID;

INSERT INTO UBF_Sys_ExtEnumType_Trl
SELECT a.Local_ID AS id,
       'zh-CN',
       b.[DisplayName] AS [name],
       b.[Description]
FROM UBF_MD_Class a
    LEFT JOIN UBF_RES_ResourceValue b
        ON CAST(a.[ID] AS VARCHAR(50)) = CAST(b.ResourceName AS VARCHAR(50))
WHERE Local_ID = @ClsID;

INSERT INTO UBF_Sys_ExtEnumValue
SELECT a.Local_ID AS id,
       '20101111',
       'YonYou',
       '20101111',
       'YonYou',
       0,
       a.[Local_Class_ID] AS ExtEnumType,
       a.[Name] AS code,
       a.DefaultValue AS evalue,
       IsSystem,
       MD_Class_ID AS ExtEnumTypeUID
FROM UBF_MD_Attribute AS a
    INNER JOIN UBF_MD_Class AS b
        ON CAST(a.MD_Class_ID AS VARCHAR(50)) = CAST(b.ID AS VARCHAR(50))
WHERE b.Local_ID = @ClsID;

INSERT INTO UBF_Sys_ExtEnumValue_Trl
SELECT a.Local_ID AS id,
       'zh-CN',
       c.[DisplayName] AS [name]
FROM UBF_MD_Attribute AS a
    INNER JOIN UBF_MD_Class AS b
        ON CAST(a.MD_Class_ID AS VARCHAR(50)) = CAST(b.ID AS VARCHAR(50))
    LEFT JOIN UBF_RES_ResourceValue c
        ON CAST(a.[ID] AS VARCHAR(50)) = CAST(c.ResourceName AS VARCHAR(50))
WHERE b.Local_ID = @ClsID;